package com.bds.btcdc.server;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.RequestUpgradeStrategy;
import org.springframework.web.socket.server.standard.TomcatRequestUpgradeStrategy;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

/**
 * @author kevin
 * @date 2018/3/28
 * @description
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketServer implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //设置h5可订阅，使用SockJS协议
        registry.addEndpoint("/market_info")
                .setAllowedOrigins("*")
                .withSockJS();
        //设置node可增加消息
        RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
        registry.addEndpoint("/market_info")
                .setAllowedOrigins("*")
                .setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy));
    }
}
